<script>
    import cryptoJS from 'crypto-js'
    import axios from 'axios'
    const encodeKey = cryptoJS.enc.Utf8.parse("157868de9dd8b25c");
    const encodeIv = cryptoJS.enc.Utf8.parse('0000000000000000');
    // const commonUrl = 'http://jsmqccgyn.ahnmc.com:1911'
    // const fileServerUrl = 'http://jsmqccgyn.ahnmc.com:1911/file'
    const commonUrl = 'http://106.15.237.231:1911'
    const fileServerUrl = 'http://106.15.237.231:1911/file'
    // const commonUrl = 'http://192.168.1.100:10118'
    // const fileServerUrl = 'http://192.168.1.00:10118/file'
    // const commonUrl = 'http://jsmqccgyn.ahnmc.com:1911'
    // const fileServerUrl = 'http://jsmqccgyn.ahnmc.com:1911/file'
    // const commonUrl = 'http://106.15.237.231:1911'
    // const fileServerUrl = 'http://106.15.237.231:1911/file'
    //文件预览
    const urlForViewer = fileServerUrl +'/pdf/web/viewer.html?file=';
    const urlForRelativePath = fileServerUrl + '/pdf/web/';
    export default {
        // 请求后台
        postData(postUrl,postPara,callbackfun){
            let url = commonUrl +'/api'+ postUrl;
            // let url = commonUrl + postUrl;
            //let url = commonUrl + postUrl;
            //let url = commonUrl +'/api'+ postUrl;
            let promise = new Promise(function(resolve, reject) {
                let params = postPara == null ? {} : postPara
                axios.post(url,params)
                    .then((response) => {
                        resolve( response);
                    })
                    .catch((error) => {                        reject( error );
                    });
            });
            if(callbackfun){
                promise.then(callbackfun).catch(function(d) {});
            }
            return promise;
        },
        exportExcel(url,data){
            return new Promise((resolve, reject) => {
                axios({
                    method: 'post',
                    url: commonUrl + '/api' + url,
                    data: data,
                    responseType: 'blob',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }).then(res => {
                    resolve(res.data);
                }).catch(err => {
                    reject(err);
                });
            })
        },
        //AES加密
        getAES(origin){
            let key = cryptoJS.enc.Utf8.parse('6933f0b1a323b98d');
            let iv  = cryptoJS.enc.Utf8.parse('0000000000000000');
            let originByte = cryptoJS.enc.Utf8.parse(origin);
            let encrypted = cryptoJS.AES.encrypt(originByte, key,
                {
                    iv:iv,
                    mode: cryptoJS.mode.CBC,
                    padding: cryptoJS.pad.ZeroPadding,
                });
            return encrypted.ciphertext.toString();
        },
        //AES加密
        getParamAES(origin){
            let key = cryptoJS.enc.Utf8.parse('6933f0b1a323b98d');
            let iv  = cryptoJS.enc.Utf8.parse('0000000000000000');
            let originByte = cryptoJS.enc.Utf8.parse(origin);
            let encrypted = cryptoJS.AES.encrypt(originByte, key,
                {
                    iv:iv,
                    mode: cryptoJS.mode.CBC,
                    padding: cryptoJS.pad.ZeroPadding,
                });
            return {param:encrypted.ciphertext.toString()}
        },
        encodePara(param){
            let srcs = cryptoJS.enc.Utf8.parse(param);
            let encrypted = cryptoJS.AES.encrypt(srcs,encodeKey,{iv:encodeIv,mode:cryptoJS.mode.CBC,padding:cryptoJS.pad.Pkcs7});
            return encrypted.ciphertext.toString().toUpperCase();
        },
        decodePara(param){
            let encryptedHexStr = cryptoJS.enc.Hex.parse(param);
            let srcs = cryptoJS.enc.Base64.stringify(encryptedHexStr);
            let decrypt = cryptoJS.AES.decrypt(srcs,encodeKey,{iv:encodeIv,mode:cryptoJS.mode.CBC,padding:cryptoJS.pad.Pkcs7 });
            let decryptedStr = decrypt.toString(cryptoJS.enc.Utf8);
            return decryptedStr.toString();
        },
        formatCurrentQuarter(){
            let myDate = new Date();
            let year = myDate.getFullYear();
            let month = myDate.getMonth() + 1;
            let quarter
            if(month >=1 && month <=3){
                quarter = 1
            }
            if(month >=4 && month <=6){
                quarter = 2
            }
            if(month >=7 && month <=9){
                quarter = 3
            }
            if(month >=10 && month <=12){
                quarter = 4
            }
            return year + '年' + quarter + '季度'
        },
        formatDateForReport(){
            let date = new Date();
            let year = date.getFullYear();
            let endDate = year+'年4季度';
            let startDate = year+'年1季度';
            return [startDate,endDate]
        },
        formatStartYear(){
            let date = new Date();
            let year = date.getFullYear();
            return year-4+'年'
        },
        formatEndYear(){
            let date = new Date();
            let year = date.getFullYear();
            return year+'年'
        },
        //用户基本信息
        queryUserBaseInfo(){
            return this.postData("/queryUserBaseInfo")
        },
        //文件下载
        transferAndDownload(file){
          let url = urlForRelativePath+file.url;
          let name = file.name;
          let a = document.createElement('a');
          a.setAttribute('download', name.substr(0,name.lastIndexOf(".")));
          a.href = url;
          document.body.appendChild(a)
          a.click();
          document.body.removeChild(a);
        },
        commonUrl,fileServerUrl,urlForViewer,urlForRelativePath
    }
</script>
